package com.keepsport.keepsportapi.controller;

import com.keepsport.keepsportapi.config.SystemConfig;
import com.keepsport.keepsportapi.dto.AliPayVipDto;
import com.keepsport.keepsportapi.service.intf.UserVipService;
import com.keepsport.keepsportapi.utils.AliPayUtil;
import com.keepsport.keepsportapi.utils.StringUtil;
import com.keepsport.keepsportapi.vo.R;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 *
 @Author YK
 @create 2022/7/5 16:34
 */
@RestController
@CrossOrigin
@Api
@RequestMapping("/api/payvip/")
public class PayVipController {
    @Resource
    private UserVipService service;
    //创建支付
    @GetMapping("createpay.do")
    public R create(HttpServletRequest request) {
        int uid = request.getIntHeader(SystemConfig.TOKEN_HEADER);
        AliPayVipDto dto = new AliPayVipDto(0.01);
        String res = AliPayUtil.createPay(dto.getNo(), dto.getMoney(),dto.getTitle());
        if (StringUtil.isNotEmpty(res)) {
            service.update(uid);
            return R.ok(res);
        } else {
            return R.fail();
        }
    }

    //查询支付状态
    @GetMapping("query.do")
    public R query(String no) {
        return R.ok(AliPayUtil.queryPay(no));
    }
    //发起退款
    @GetMapping("refund.do")
    public R refund(String no,double money) {
        if (AliPayUtil.refound(no,money)) {
            return R.ok();
        } else {
            return R.fail();
        }
    }
}
